package com.marktotop.controller;

import com.marktotop.entity.LoginLog;
import com.marktotop.response.ResultBean;
import com.marktotop.service.ILoginLogService;
import com.marktotop.vo.AdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * LoginLogController
 *
 * @author WXLD
 * @Data 2021/7/1
 * @Since 1.0
 */
@Api(tags = "登入日志相关接口")
@RestController
@RequestMapping("/system/loginLog")
public class LoginLogController {

    @Autowired
    private ILoginLogService loginLogService;


    /**
     * 日志列表
     *
     * @return
     */
    @ApiOperation(value = "日志列表", notes = "登入日志列表，模糊查询")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultBean findLoginLogList(@RequestParam(value = "pageNum", defaultValue = "1") Integer currentPage,
                                       @RequestParam(value = "pageSize") Integer size,
                                         LoginLog loginLog) {

        return loginLogService.findLoginLogList(currentPage, size, loginLog);
    }


    /**
     * 删除日志
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除日志")
    @RequestMapping(value = "/del/{id}",method = RequestMethod.DELETE)
    public ResultBean delete(@PathVariable Long id) {
        return loginLogService.deleteLog(id);
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除登入日志")
    @RequestMapping(value = "/",method = RequestMethod.DELETE)
    public ResultBean batchDeleteLogs(Long[] ids) {
        if(loginLogService.batchDeleteLogs(Arrays.asList(ids))){
            return ResultBean.ok().message("批量删除登入日志操作成功！");
        }
        return ResultBean.error().message("批量删除登入日志操作失败！");
    }


    /**
     * 登入报表
     * @return
     */
    @RequestMapping(value = "/loginReport",method = RequestMethod.POST)
    @ApiOperation(value = "登入报表",notes = "用户登入报表")
    public ResultBean loginReport(@RequestBody AdminVO adminVO){
        List<Map<String,Object>> mapList= loginLogService.loginReport(adminVO);
        Map<String,Object> map=new HashMap<>();
        adminVO.setName(null);
        List<Map<String,Object>> meList= loginLogService.loginReport(adminVO);
        map.put("me",mapList);
        map.put("all",meList);
        return ResultBean.ok().data("result",map);
    }
}
